package dao;

import java.io.File;
import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import static org.hibernate.criterion.Example.create;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sun.org.apache.bcel.internal.generic.NEW;

/**
 * A data access object (DAO) providing persistence and search support for
 * Chapter entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see dao.Chapter
 * @author MyEclipse Persistence Tools
 */

public class ChapterDAO extends BaseHibernateDAO {
    private static final Logger log = LoggerFactory.getLogger(ChapterDAO.class);
    // property constants
    public static final String COURSEID = "courseid";
    public static final String NAME = "name";
    public static final String STRUCTURECHART = "structurechart";
    public static final String DEMAND = "demand";

    public boolean save(Chapter transientInstance) {
	return save(log, "saving Chapter instance", transientInstance);	
    }

    public boolean delete(Chapter persistentInstance) {
	return delete(log, "deleting Chapter instance", persistentInstance);
    }

    public Chapter findById(java.lang.String id) {
	log.debug("getting Chapter instance with id: " + id);
	try {
	    Chapter instance = (Chapter) getSession().get("dao.Chapter", id);
	    return instance;
	} catch (RuntimeException re) {
	    log.error("get failed", re);
	    throw re;
	}
    }

    public List<Chapter> findByExample(Chapter instance) {
	log.debug("finding Chapter instance by example");
	try {
	    List<Chapter> results = (List<Chapter>) getSession()
		    .createCriteria("dao.Chapter").add(create(instance)).list();
	    log.debug("find by example successful, result size: "
		    + results.size());
	    return results;
	} catch (RuntimeException re) {
	    log.error("find by example failed", re);
	    throw re;
	}
    }

    public List findByProperty(String propertyName, Object value) {
	log.debug("finding Chapter instance with property: " + propertyName
		+ ", value: " + value);
	try {
	    String queryString = "from Chapter as model where model."
		    + propertyName + "= ?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setParameter(0, value);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find by property name failed", re);
	    throw re;
	}
    }

    public List<Chapter> findByCourseid(Object courseid) {
	return findByProperty(COURSEID, courseid);
    }

    public List<Chapter> findByName(Object name) {
	return findByProperty(NAME, name);
    }

    public List<Chapter> findByStructurechart(Object structurechart) {
	return findByProperty(STRUCTURECHART, structurechart);
    }

    public List<Chapter> findByDemand(Object demand) {
	return findByProperty(DEMAND, demand);
    }

    public List findAll() {
	log.debug("finding all Chapter instances");
	try {
	    String queryString = "from Chapter";
	    Query queryObject = getSession().createQuery(queryString);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    log.error("find all failed", re);
	    throw re;
	}
    }

    public List<String> getChaptersByCourseId(String courseId){
	try {
	    String queryString = "select chapterid from Chapter where courseid=?";
	    Query queryObject = getSession().createQuery(queryString);
	    queryObject.setString(0, courseId);
	    return queryObject.list();
	} catch (RuntimeException re) {
	    throw re;
	}
    }
    public Chapter merge(Chapter detachedInstance) {
	log.debug("merging Chapter instance");
	try {
	    Chapter result = (Chapter) getSession().merge(detachedInstance);
	    log.debug("merge successful");
	    return result;
	} catch (RuntimeException re) {
	    log.error("merge failed", re);
	    throw re;
	}
    }

    public void attachDirty(Chapter instance) {
	attachDirty(log, "attaching dirty Chapter instance", instance);
    }
    
    public boolean update(Chapter instance) {
	return update(log, "updating Chapter instance", instance);
    }

    public void attachClean(Chapter instance) {
	log.debug("attaching clean Chapter instance");
	try {
	    getSession().lock(instance, LockMode.NONE);
	    log.debug("attach successful");
	} catch (RuntimeException re) {
	    log.error("attach failed", re);
	    throw re;
	}
    }
    
    public static void main(String[] args){
	ChapterDAO chapterDAO = new ChapterDAO();
	String pathString = new File("../image/����.jpg").getAbsolutePath();
	//File file = new File("../image/����.jpg");
	Chapter chapter = new Chapter("01001", "01", "��һ��", pathString, "����˭����ʶ�Լ�");
	Chapter chapter2 = new Chapter();
	chapter2.setChapterid("01001");
	chapter2.setCourseid("01");
	chapterDAO.delete(chapter2);
	System.out.println("�����ɹ���");
    }
}